package com.zhdl.network.strategy.modbusrtu;

import com.project.command.faq.event.behavior.BehaviorPerform;
import com.project.command.instruction.bo.SingleInstruction;
import com.zhdl.common.util.IdUtil;
import com.zhdl.network.modbusrtu.after.ModbusAfterService;
import com.zhdl.network.modbusrtu.analysis.ModbusAnalysis;
import com.zhdl.network.strategy.common.NetworkStrategy;

import static com.project.command.instruction.strategy.abs.AbsDic.INSTRUCTION_DEFAULT_HEX;

/**
 * Hardware-ModbusRtu指令发送
 */
public class ModbusRtuStrategy implements NetworkStrategy {

    public final BehaviorPerform behaviorPerform;
    public String Port;

    public ModbusRtuStrategy(String Port, BehaviorPerform behaviorPerform) {
        this.Port = Port;
        this.behaviorPerform = behaviorPerform;
    }

    @Override
    public void sendCmd(String cmd) {
        SingleInstruction instruction = new SingleInstruction();
        instruction.setInstructionId(IdUtil.getUUID_8());
        instruction.setInstruction(cmd);
        instruction.setBeforeInstructionName(INSTRUCTION_DEFAULT_HEX);
        //默认使用com.zhdl.instruction.strategy包下后置策略
        instruction.setAfterInstructionName(ModbusAfterService.MODBUS_AFTER_SERVICE);
        //客户端通信地址, 串口为数字, 网口为ip+端口       数据来源 : port_comm 或 port_socket 表
        instruction.setPortKey(Port);
        //设置协议  数据来源 : protocols 表
        instruction.setProtocols(ModbusAnalysis.MODBUS_ANALYSIS);
        behaviorPerform.perform(instruction);
    }
}
